package com.link.play.fit.ai.ui.main.fragment.homeFragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.core.app.ActivityOptionsCompat
import androidx.core.util.Pair
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseFragment
import com.link.play.fit.ai.base.goToAc
import com.link.play.fit.ai.bean.BaseCalendarData
import com.link.play.fit.ai.bean.CalendarStatusData
import com.link.play.fit.ai.databinding.HomeChildLayout1Binding
import com.link.play.fit.ai.ui.adjust.AdjustGoalsActivity
import com.link.play.fit.ai.ui.main.MainViewModel
import com.link.play.fit.ai.utils.NumberAnimator
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlin.math.abs

/**
 *Create by SleepDog on 2025-03-24
 */
class HomeChildFragment1 : BaseFragment<HomeChildLayout1Binding>(HomeChildLayout1Binding::inflate) {
    private val viewModel: MainViewModel by lazy {
        ViewModelProvider(requireActivity())[MainViewModel::class.java]
    }
    private var mconsumeCal = 0
    private var mCal = -1
    private var calendarStatusData: CalendarStatusData? = null
//    private var userGuide: UserGuide? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initListener()
        initLiveData()
        EventBus.getDefault().register(this)
    }


    @SuppressLint("SetTextI18n")
    private fun initLiveData() {
        viewLifecycleOwner.lifecycleScope.launch {
            viewModel.recentlyGoalData.collect {
                showData()
            }
        }

    }

    private fun initListener() {
        binding.contentLayout.setOnClickListener {
            toGoalsPage()
        }
        binding.proteinLayout.setOnClickListener {
            toGoalsPage()

        }
        binding.carbsLayout.setOnClickListener {
            toGoalsPage()

        }
        binding.fatsLayout.setOnClickListener {
            toGoalsPage()
        }
    }

    private fun toGoalsPage() {
//        viewModel.onclickTapScan("toJournal")
        goToAc(
            AdjustGoalsActivity::class.java, ActivityOptionsCompat.makeSceneTransitionAnimation(
                requireActivity(),
                Pair(
                    binding.flCal, getString(R.string.Calorie_goal)
                ),
                Pair(
                    binding.flProtein, getString(R.string.Protein_goal)
                ),
                Pair(
                    binding.flCarb, getString(R.string.Carb_goal)
                ),
                Pair(
                    binding.flFat, getString(R.string.Fat_goal)
                )
            ).toBundle()
        ) {
            putExtra("data", viewModel.getCalendarData())
        }
    }


    fun setCalendarData(data: CalendarStatusData) {
        calendarStatusData = data
        viewModel.setCalendarData(data)
        showData()
    }

    private fun showData() {
        calendarStatusData = viewModel.getCalendarData()
        calendarStatusData?.let {
            val mCalories = it.caloriesDaily - it.consumeCaloriesDaily
            val mProtein = it.proteinDaily - it.consumeProteinDaily
            val mCarbs = it.carbsDaily - it.consumeCarbsDaily
            val mFats = it.fatsDaily - it.consumeFatsDaily

            mconsumeCal = it.consumeProteinDaily
            if (mCalories >= 0) {
                NumberAnimator.animateNumberChange(binding.kllValueText, binding.kllValueText.text.toString().toInt(), mCalories)
                binding.kllDesText.text = getString(R.string.Calories_left)
            } else {
                NumberAnimator.animateNumberChange(binding.kllValueText, binding.kllValueText.text.toString().toInt(), abs(mCalories))
                binding.kllDesText.text = getString(R.string.Calories_over)
            }
            if (mProtein >= 0) {
                binding.proteinValueText.text = "$mProtein"
                binding.proteinText.text = getString(R.string.Protein_left)
            } else {
                binding.proteinValueText.text = "${abs(mProtein)}"
                binding.proteinText.text = getString(R.string.Protein_over)
            }
            if (mCarbs >= 0) {
                binding.carbsValueText.text = "$mCarbs"
                binding.carbsText.text = getString(R.string.Carbs_left)
            } else {
                binding.carbsValueText.text = "${abs(mCarbs)}"
                binding.carbsText.text = getString(R.string.Carbs_over)
            }
            if (mFats >= 0) {
                binding.fatsValueText.text = "$mFats"
                binding.fatsText.text = getString(R.string.Fats_left)
            } else {
                binding.fatsValueText.text = "${abs(mFats)}"
                binding.fatsText.text = getString(R.string.Fats_over)
            }

            binding.kllProgressbar.max = it.caloriesDaily
            binding.kllProgressbar.progress = it.consumeCaloriesDaily

            binding.proteinProgress.max = it.proteinDaily
            binding.proteinProgress.progress = it.consumeProteinDaily

            binding.carbsProgress.max = it.carbsDaily
            binding.carbsProgress.progress = it.consumeCarbsDaily

            binding.fatsProgress.max = it.fatsDaily
            binding.fatsProgress.progress = it.consumeFatsDaily
        }

    }

    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(bean: CalendarStatusData) {
        viewModel.setCalendarData(bean)
        setCalendarData(bean)
        mCal = bean.caloriesDaily
        EventBus.getDefault().post(BaseCalendarData(type = "", data = bean))

    }

    override fun onDestroyView() {
        super.onDestroyView()
        EventBus.getDefault().unregister(this)

    }
}